<?php
namespace app\user\controller;
use think\Db;
use think\Loader;
use app\common\logic\Sms;
class Verificationcode extends Common
{
    //发送腾讯云验证码
    private function qcloudsmscode($oid,$number)
    {
        $code = rand(100000,999999);
        //查询配置腾讯云短信配置信息
        $apiinfo = Db::name('verificationcode')
            ->where('oid',$oid)
            ->where('types',1)
            ->where('provider',1)
            ->find();

        //重新拼接模板内容
        $param = $apiinfo['param'];
        $param = str_replace('{'.$apiinfo['codename'].'}',$code,$param);
        $param = str_replace('{'.$apiinfo['timename'].'}',$apiinfo['time'],$param );
        $param = '【'.$apiinfo['signname'].'】'.$param;


        //引入腾讯短信SDK
        Loader::import('Qcloud.SmsSender');
        $singleSender = new \Qcloud\Sms\SmsSingleSender($apiinfo['appid'], $apiinfo['appkey']);
        
        $result = $singleSender->send($number,$param );
        $rsp = json_decode($result,true);
        //判断是否发送成功，发送成功则将验证码写入缓存。
        if($rsp['result']==0&$rsp['errmsg']=='OK'){
            cache('VerificationCode|oid='.$oid.'+number='.$number, $code, $apiinfo['time']*60);
            $res['errmsg'] = '发送成功！';
            $res['code'] = 1 ; 
            return  $res;
        }elseif($rsp['result']==1025){
            $res['errmsg'] = '发送数量超过限制！';
            $res['code'] = 0; 
            return  $res;
        }else{
            $res['error'] = $rsp;
            $res['errmsg'] = '验证码发送错误,请联系客服';
            $res['code'] = 0; 
            return  $res;
        }
    }
    //发送短信验证码
    public function sendsmscode()
    {

        $oid = 1;
        $number = input('param.phone');
        $smsObj = new Sms();
        $result = $smsObj->sendSms($number);
        return $result;
    }
    //发送语音验证码
    public function sendvoicecode()
    {



    }
    //检查验证码
    public function  checkcode(){
        $oid = input('param.oid');
        $number = input('param.phone');
        $openid = mwdecrypt(input('param.openid'));
        $inputcode = input('param.code');

        $code = cache('VerificationCode|oid='.$oid.'+number='.$number);

        if($inputcode==$code&&strlen($inputcode)==6){
            //验证成功
            Db::name('user')->where('openid',$openid)->update(['mobile' => $number]);
            
            return  1;
        }else{
            //验证失败
            return  0;
        }

    }

}

